package com.nikitova.craps3d.view 
{
	import caurina.transitions.Tweener;
	import caurina.transitions.properties.CurveModifiers;

	import com.carlcalderon.arthropod.Debug;
	import com.nikitova.craps3d.constants.statemachine.StateActions;
	import com.nikitova.craps3d.constants.statemachine.StateNames;
	import com.nikitova.craps3d.constants.statemachine.StateTransitions;

	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.view.layer.ViewportBaseLayer;
	import org.puremvc.as3.multicore.interfaces.*;
	import org.puremvc.as3.multicore.patterns.mediator.*;
	import org.puremvc.as3.multicore.utilities.statemachine.StateMachine;

	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;

	/**
	 * @author schranz
	 */
	public class CameraMediator extends Mediator 
	{
		public static const NAME        : String = "CameraMediator";
		
		private var __cameraTarget 			        : DisplayObject3D  = new DisplayObject3D();
		
		private var __currentTarget 	 :	DisplayObject3D;
		private var __currentState	 	 :	String;
		private var __stage:Stage;
		
		private var __camPitch:Number = 90;
		private var __camYaw:Number = 270;
		private var __previousMouseX:Number;
		private var __previousMouseY:Number;
		private var __easePitch:Number = 90;
		private var __easeYaw:Number = 270;
		
		//private var __gameView3DMediator : GameView3DMediator;

		public function CameraMediator(component : DisplayObject3D, name : String = NAME)
		{
			super(name, component);
		}

		override public function onRegister() : void
		{
			//__gameView3DMediator = facade.retrieveMediator(GameView3DMediator.NAME) as GameView3DMediator;
			camera.target = __cameraTarget;
			CurveModifiers.init();
		}

		override public function listNotificationInterests() : Array  
		{
			return [
										
						StateTransitions.SHOW_BET_STATE,
						StateTransitions.SHOW_ROLL_STATE,
						
					];
		}

		override public function handleNotification( note : INotification ) : void
		{
			switch ( note.getName()) 
			{
				case StateTransitions.SHOW_BET_STATE:
					__stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
					__currentTarget = camera.target;
					if (!Tweener.isTweening(camera))
					{
						Tweener.addTween(__cameraTarget,{
										x:0,
										y:0,
										z:100,
										time:1,
										transition:"linear"
										});		
						Tweener.addTween(camera,{
										x:-50,
										y:400,
										z:80,
										localRotationZ:120,
										_bezier:{x:-600, y:400, z:0},
										time:1,
										transition:"linear",
										onComplete:setCurrentState,
										onCompleteParams:[StateNames.BET_STATE]
										});
						
					}
				break;
				case StateTransitions.SHOW_ROLL_STATE:
					if (!Tweener.isTweening(camera))
					{
						Tweener.addTween(__cameraTarget,{
										x:0,
										y:0,
										z:0,
										time:1,
										transition:"easeInSine",
										onComplete:setCurrentState,
										onCompleteParams:[StateNames.ROLL_STATE]
										});		
						Tweener.addTween(camera,{
										x:0,
										y:300,
										z:-500,
										localRotationZ:0,
										_bezier:{x:-600, y:300, z:0},
										time:1,
										transition:"easeInSine",
										onComplete:setCurrentState,
										onCompleteParams:[StateNames.ROLL_STATE]
										});
						
					}
				break;
				
				
			}
		}
		
		private function setCurrentState(state:String):void
		{
			__currentState = state;
			Debug.log("Current state "+__currentState);
			if(__currentState == StateNames.ROLL_STATE)
			{
			   __previousMouseX = __stage.mouseX;
			   __camPitch = camera.rotationX;
			   __stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
			}
		}
		private function handleMouseUp(event : MouseEvent) : void
		{
			
			if(event.target is ViewportBaseLayer)
			{
				event.stopImmediatePropagation();
				if(__currentState == StateNames.BET_STATE)
					sendNotification(StateMachine.ACTION,null,StateActions.GO_ROLL_STATE);
				else	
					sendNotification(StateMachine.ACTION,null,StateActions.GO_BET_STATE);
			}
		}
		private function onMouseMove(event:MouseEvent):void
		{
			var differenceX:Number = __stage.mouseX - __stage.stageWidth * 0.5;
			if(Math.abs(differenceX) < 200)
			{
				camera.x = differenceX;
				if(differenceX<0)
				 camera.z = (-1)*differenceX - 500;
				else
				 camera.z = differenceX - 500;
			}
			
		}
		public function set stage(value : Stage) : void
		{
			__stage = value;
			__stage.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp, true, 0, true);
		}
		
		public function get camera() : Camera3D
		{
			return viewComponent as Camera3D;
		}

	}
}
